<?php
class omeanalysts_mdl_ome_ivantextdetails extends dbeav_model{

    //var $has_export_cnf = true;

    var $export_name = 'ivantext查询';

    public function table_name($real=false){
        $table_name = 'ome_delivery';
        if($real){
            return kernel::database()->prefix.$table_name;
        }else{
            return $table_name;
        }
    }

    public function count($filter=null){

        $where = $this->_filter($filter); 

        $sql = "SELECT count(*) as _count from sdb_ome_ivantext id  
                inner join sdb_ome_ivantextdetails idl on id.id=idl.ivantext_id
                inner join sdb_ome_shop f on id.shop_id=f.shop_id
                    where  ".$where." ";
        $row = $this->db->select($sql);
        return intval($row[0]['_count']);
    }

    public function getlist($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null){
  

        if(!$filter['time_from'] && !$filter['time_to'])
        {
            return false;
        }else{           
            $where = $this->_filter($filter); 
            $sql = "SELECT f.shop_bn,f.name shop_name,begin_date,end_date,create_date,Code,status,idl.bn,idl.name,idl.nums,idl.price,idl.amount 
                from sdb_ome_ivantext id 
                inner join sdb_ome_ivantextdetails idl on id.id=idl.ivantext_id
                inner join sdb_ome_shop f on id.shop_id=f.shop_id
                where  ".$where." "; 
            $rows = $this->db->selectLimit($sql,$limit,$offset);
            return $rows;
        }
        
    }
    
    public function searchOptions(){
        $parentOptions = parent::searchOptions();
        $childOptions = array(
            'order_bn'=>app::get('base')->_('订单号'),
        );
        return $Options = array_merge($parentOptions,$childOptions);
    }

    public function _filter($filter,$tableAlias=null,$baseWhere=null){

         $where = array(1);

       if(empty($filter['time_from']))
        {
          $where[]=' 1=2 ';
        }
        if(isset($filter['time_from']) && $filter['time_from']){
            $where[] = ' id.create_date >='.strtotime($filter['time_from']);
        }
        if(isset($filter['time_to']) && $filter['time_to']){
            $where[] = 'id.create_date <'.(strtotime($filter['time_to'])+86400);
        }
         
        if(isset($filter['shop_id'])   && $filter['shop_id']){
            $where[] = ' f.shop_id  =\''.addslashes($filter['shop_id']).'\'';
        }
        if(!$filter['shop_id'] && $filter['shop_ids']){
            $where[] = ' f.shop_id IN (\''.implode('\',\'', $filter['shop_ids']).'\')';
        }
        if(isset($filter['bn']) && $filter['bn']){
            $where[] = ' idl.bn =\''.addslashes($filter['bn']).'\'';
        }
        if(isset($filter['status']) && $filter['status']){
            $where[] = ' id.status =\''.addslashes($filter['status']).'\'';
        }
        unset($filter['shop_id']);
        
        return implode($where,' AND ');
    }

    public function get_schema(){
        $schema = array (
            'columns' => array (
                'shop_bn' =>
                array (
                  'type' => 'varchar(255)',
                  'required' => true,
                  'label' => '店铺编码',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '120',
                ), 
                'shop_name' =>
                array (
                  'type' => 'varchar(255)',
                  'required' => true,
                  'label' => '店铺',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '120',
                ), 
                'begin_date' =>
                array (
                  'type' => 'time',
                  'required' => true,
                  'label' => '开始时间',
                  'is_title' => true,
                  'default_in_list' => true,
                  'width' => 70,
                  'searchtype' => 'has',
                  'editable' => false,
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                ), 
                'create_date' =>
                array (
                  'type' => 'time',
                  'required' => true,
                  'label' => '创建时间',
                  'is_title' => true,
                  'default_in_list' => true,
                  'width' => 70,
                  'searchtype' => 'has',
                  'editable' => false,
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                ),
                'end_date' =>
                array (
                  'type' => 'time',
                  'required' => true,
                  'label' => '结束时间',
                  'is_title' => true,
                  'default_in_list' => true,
                  'width' => 70,
                  'searchtype' => 'has',
                  'editable' => false,
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                ),
                 'Code' =>
                array (
                  'type' => 'varchar(30)',
                  'label' => '编号',
                  'editable' => false,
                  'is_title' => true,
                ),          
                 'bn' =>
                array (
                  'type' => 'varchar(30)',
                  'label' => '物料',
                  'editable' => false,
                  'is_title' => true,
                ),   
                 'name' =>
                array (
                  'type' => 'varchar(200)',
                  'required' => true,
                  'label' => '物料名称',
                  'is_title' => true,
                  'default_in_list' => true,
                  'width' => 260,
                  'searchtype' => 'has',
                  'editable' => false,
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                ),   
                 'price' =>
                array (
                  'type' => 'money',
                  'label' => '价格',
                  'width' => 70,
                  'required' => true,
                  'default' => 0,
                  'editable' => false,
                ),
                'nums' =>
                array (
                  'type' => 'number',
                  'label' => '购买量',
                  'width' => 60,
                  'is_title' => true,
                  'default_in_list' => true,
                  'searchtype' => 'has',
                  'editable' => false,
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                ),    
              'status' =>
              array (
                'type' =>
                array (
                  'unconfirmed' => '新建',
                  'confirmed' => '已审核',
                  'cancel' => '取消',
                ),

                'default' => 'unconfirmed',
                'required' => true,
                'label' => '状态',
                'width' => 70,
                'editable' => false,
                'filtertype' => 'yes',
                'filterdefault' => true,
                'in_list' => true,
                'default_in_list' => false,
              ),
              'amount' =>
                array (
                  'type' => 'money',
                  'label' => '总金额',
                  'width' => 70,
                  'required' => true,
                  'default' => 0,
                  'editable' => false,
                ),          
              
            ),/*f.shop_bn,f.name shop_name,begin_date,end_date,create_date,Code,status,idl.bn,idl.name,idl.nums,idl.price,idl.amount */
            'idColumn' => 'name',
            'in_list' => array (
                0 => 'shop_bn',
                1 => 'shop_name',
                2 => 'begin_date',
                3 => 'end_date',
                4 => 'create_date',
                5 => 'Code',
                6 => 'status',
                7 => 'bn',
                8 => 'name',
                9 => 'nums',
                10 => 'price',
                11 => 'amount', 
            ),
            'default_in_list' => array (
                0 => 'shop_bn',
                1 => 'shop_name',
                2 => 'begin_date',
                3 => 'end_date',
                4 => 'create_date',
                5 => 'Code',
                6 => 'status',
                7 => 'bn',
                8 => 'name',
                9 => 'nums',
                10 => 'price',
                11 => 'amount',    
            ),
        );
        return $schema;
    }

    /**
     * 获得日志类型(non-PHPdoc)
     * @see dbeav_model::getLogType()
     */
    public function getLogType($logParams) {
        $type = $logParams['type'];
        $logType = 'none';
        if ($type == 'export') {
            $logType = $this->exportLogType($logParams);
        }
        elseif ($type == 'import') {
            $logType = $this->importLogType($logParams);
        }
        return $logType;
    }
    /**
     * 导出日志类型
     * @param Array $logParams 日志参数
     */
    public function exportLogType($logParams) {
        $params = $logParams['params'];
        $type = 'report';
        if ($logParams['app'] == 'omeanalysts' && $logParams['ctl'] == 'ome_delivery') {
            $type .= '_purchaseReport_expressAnalysis';
        }
        $type .= '_export';
        return $type;
    }
    /**
     * 导入操作日志类型
     * @param Array $logParams 日志参数
     */
    public function importLogType($logParams) {
        $params = $logParams['params'];
        $type = 'report';
        if ($logParams['app'] == 'omeanalysts' && $logParams['ctl'] == 'ome_delivery') {
            $type .= '_purchaseReport_expressAnalysis';
        }
        $type .= '_import';
        return $type;
    }

    function io_title( $filter, $ioType='csv' ){

        switch( $filter ){
            case 'delivery':
                $this->oSchema['csv'][$filter] = array(
                    '*:店铺' => 'shop_bn',
                    '*:店铺名称' => 'shop_name',
                    '*:订单日期' => 'create_date',
                    '*:开始日期' => 'begin_date',
                    '*:结束日期' => 'end_date',
                    '*:物料' => 'bn',
                    '*:物料名称' => 'name',
                    '*:价格' => 'price',
                    '*:购买量' => 'issuenum',
                    '*:总金额' => 'amount',
                    '*:状态' => 'status',
                );
                break;
        }
        $this->ioTitle[$ioType][$filter] = array_keys( $this->oSchema[$ioType][$filter] );
        return $this->ioTitle[$ioType][$filter];
    }

    //根据查询条件获取导出数据
    public function getExportDataByCustom($fields, $filter, $has_detail, $curr_sheet, $start, $end){
          //根据选择的字段定义导出的第一行标题
          if($curr_sheet == 1){

              $title = array();
              foreach($this->io_title('delivery') as $k => $v ){
                  $title[] = mb_convert_encoding($v, 'GBK', 'UTF-8');
              }

              $data['content']['main'][0] = '"'.implode('","',$title).'"';
          }

          $where = $this->_filter($filter);
              /*f.shop_bn,f.name shop_name,begin_date,end_date,create_date,Code,status,idl.bn,idl.name,idl.nums,idl.price,idl.amount */
      $sql = "SELECT f.shop_bn,f.name shop_name,FROM_UNIXTIME(id.create_date, '%Y-%m-%d %H:%i:%S') as createtime,idl.bn,idl.`name` name,idl.nums,idl.price,id.status,idl.amount,
          begin_date,end_date,Code
          from sdb_ome_ivantext id 
          inner join sdb_ome_ivantextdetails idl on id.id=idl.ivantext_id
          inner join sdb_ome_shop f on id.shop_id=f.shop_id
                where  ".$where." "; 
                $status=
                array (
                  'unconfirmed' => '新建',
                  'confirmed' => '已审核',
                  'cancel' => '取消',
                );
            $omedelivery = $this->db->selectLimit($sql,$end,$start);
            foreach ($omedelivery as $key => $value) 
            {
              $omedelivery[$key]=$status[$value['status']];
            }
            foreach ($omedelivery as $k => $aFilter) {
                $exptmp_data = array();
                foreach ($aFilter as $col => $val) {
                    $aFilter[$col] = mb_convert_encoding($aFilter[$col], 'GBK', 'UTF-8');
                    $exptmp_data[] = $aFilter[$col];
                }

                $data['content']['main'][] = implode(',', $exptmp_data);
            }
            return $data;
        
    }
}
